package ca.unbc.cpsc672;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import ca.unbc.cpsc672.fuzzy.IOUtils;
import ca.unbc.cpsc672.rcmd.DBUtils;

public class ResetDialog extends Dialog {

	public ResetDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(GUIUtils.buildGridData());
		String str = "Reset all data in Knowledge Base, Fuzzy Sets and Rules?";
		new Label(composite, SWT.NULL).setText(str);

		return super.createContents(parent);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Reset to Default");
	}

	@Override
	protected void okPressed() {
		try {
			copyDefault(IOUtils.FS_FN);
			copyDefault(IOUtils.R_FN);
			DBUtils.reset();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.okPressed();
	}

	private void copyDefault(String name) throws IOException {
		Files.copy(new File("default\\" + name).toPath(),
				new File(name).toPath(), StandardCopyOption.REPLACE_EXISTING);
	}
}